using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SpoonCore.Storage;

namespace SpoonCore.Message.Storage
{
	class StorageMessage : IMessage
	{
		public enum StorageTypeValue : byte { Read, Write, Refresh }
		public enum StorageStatusValue : byte { Request, Response }
		private StorageTypeValue _storageType;
		private StorageStatusValue _storageStatus;
		private SpoonIdentifier _id;
		private byte[] _data;

		public StorageTypeValue StorageType { get { return _storageType; } set { _storageType = value; } }
		public StorageStatusValue StorageStatus { get { return _storageStatus; } set { _storageStatus = value; } }
		public byte[] Data { get { return _data; } set { _data = value; } }
		public SpoonIdentifier Id { get { return _id; } set { _id = value; } }

		#region IMessage Members

		public int MsgId
		{
			get { return 10; }
		}

		public byte[] GetBytes()
		{
			MemoryStream stream = new MemoryStream();
			BinaryWriter bw = new BinaryWriter(stream);

			bw.Write((byte)_storageType);
			bw.Write((byte)_storageStatus);
			bw.Write(_id.GetBytes());
			bw.Write(_data.Length);
			bw.Write(_data);

			return stream.ToArray();
		}

		public void SetBytes(SpoonClient client, BinaryReader br)
		{
			_storageType = (StorageTypeValue)br.ReadByte();
			_storageStatus = (StorageStatusValue)br.ReadByte();
			_id = new SpoonIdentifier(br);
			int len = br.ReadInt32();
			_data = br.ReadBytes(len);
		}

		public IMessage Clone()
		{
			return new StorageMessage();
		}

		public SpoonCore.Message.Info.MessageInfo Info
		{
			get { return null; }
			set { }
		}

		#endregion
	}
}